using SDK.Scripts.App;
using SDK.Scripts.Module;
using SDK.Scripts.Utils;
using SimpleJson;

namespace FrameWork.Scripts.Module
{
	public class GFW_Pay : BaseModule
	{
		public delegate void PayFunc(bool ret, ItemInfo item);

		private enum CallBackCode
		{
			BACK_FAILED,
			BACK_SUCCESS,
			BACK_CANCLE
		}

		private PayFunc _payBack;

		public GFW_Pay()
		{
			GameSDK.GamePlat.gamePlatEvent += OnEventBack;
		}

		public void OnEventBack(string methonName, int code, string data)
		{
			if (!(methonName == "pay"))
			{
				return;
			}
			bool flag = code == 1;
			ItemInfo item = new ItemInfo(-1);
			if (flag)
			{
				JsonObject jsonObject = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(data);
				if (jsonObject != null)
				{
					if (jsonObject.ContainsKey("goodsid"))
					{
						item.id = int.Parse(jsonObject["goodsid"].ToString());
					}
					if (jsonObject.ContainsKey("goodscount\t"))
					{
						item.count = int.Parse(jsonObject["goodscount\t"].ToString());
					}
					if (jsonObject.ContainsKey("price"))
					{
						item.price = float.Parse(jsonObject["price"].ToString());
					}
					if (jsonObject.ContainsKey("goodsdesc"))
					{
						item.desc = jsonObject["goodsdesc"].ToString();
					}
					if (jsonObject.ContainsKey("goodsname"))
					{
						item.name = jsonObject["goodsname"].ToString();
					}
					if (jsonObject.ContainsKey("extra"))
					{
						item.extra = jsonObject["extra"].ToString();
					}
				}
			}
			if (_payBack != null)
			{
				_payBack(flag, item);
				_payBack = null;
			}
		}

		public void Pay(int id, int count, string desc, string name, float price, JsonObject payInfo = null, PayFunc cb = null)
		{
			if (payInfo == null)
			{
				payInfo = new JsonObject();
			}
			payInfo.Add("price", price.ToString());
			payInfo.Add("goodscount", count);
			payInfo.Add("goodsdesc", desc);
			payInfo.Add("goodsname", name);
			payInfo.Add("goodsid", id);
			_payBack = cb;
			string text = SimpleJson.SimpleJson.SerializeObject(payInfo);
			Debuger.Log("pay info = " + text);
			GameSDK.GamePlat.Pay(text);
		}
	}
}
